{include file="header/index" /}
<div class="newContentBox">
    <form class="layui-form newListPart" id="myform" enctype="multipart/form-data">
        <!-- 隐藏域 关系id -->
        <input type="hidden" id="id" name="id" value="{$id}">
        <div class="newPart">
            <!-- 基本信息 -->
            <div class="newTitleBox">
                <span class="layui-inline newListTitle">基本信息</span>
                <a class="newBtnDefault newBtnGray" onclick="returnBack(this)"> <i class="fa fa-mail-reply"></i>返回</a>
            </div>
            <div class="newContent">
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">姓名：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input" readonly id="name" name="name" type="text" value="" placeholder="请输入姓名" />
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">电话：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input" readonly id="phone" name="phone" type="text" value="" placeholder="请输入电话" />
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">合同编号：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input" readonly id="contract_no" name="contract_no" type="text" value="" placeholder="请输入合同编号" />
                    </div>
                </div>
                <!-- 账户信息 -->
                <div class="newTitleBox">
                    <span class="layui-inline newListTitle">结算信息</span>
                </div>
                <div class="newContent">
                    <div class="layui-form-item cover_layui_f_i isPushBox">
                        <label class="layui-form-label cover_layui_f_l">推荐人：</label>
                        <div class="layui-input-block cover_layui_i_b statement_box">
                            <select class="statement_id" name="push_apply_id" id="push_apply_id" lay-search lay-filter="selectStatement">
                                <option value="">--请选择或搜索选择--</option>
                            </select>
                            <input class="statement_name" type="hidden" name="push_apply_name" id="push_apply_name">
                        </div>
                    </div>
                    <div class="layui-form-item cover_layui_f_i">
                        <label class="layui-form-label cover_layui_f_l">讲解：</label>
                        <div class="layui-input-block cover_layui_i_b statement_box">
                            <select class="statement_id" name="explainer_id" id="explainer_id" lay-search lay-filter="selectStatement">
                                <option value="">--请选择或搜索选择--</option>
                            </select>
                            <input class="statement_name" type="hidden" name="explainer_name" id="explainer_name">
                        </div>
                    </div>
                    <div class="layui-form-item cover_layui_f_i">
                        <label class="layui-form-label cover_layui_f_l">市场推广：</label>
                        <div class="layui-input-block cover_layui_i_b statement_box" style="display: flex;">
                            <select class="statement_id" name="manage_id" id="manage_id" lay-search lay-filter="selectStatement">
                                <option value="">--请选择或搜索选择--</option>
                            </select>
                            <input class="statement_name" type="hidden" id="manage_name" name="manage_name">
                            <select class="statement_state" name="five_manage_state" id="five_manage_state">
                                <option value="2">不合格</option>
                                <option value="1">合格</option>
                            </select>
                            <input class="statement_money layui-input" id="manage_money" name="manage_money" type="text" style="width:33%;" type="text" value="0" placeholder="请输入比例" onblur="(this.v=function(){this.value=checkMoney(this.value)}).call(this)">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item ">
                    <div class="layui-input-block cover_layui_i_b">
                        
                        {if condition="isset($edit) && $edit"}
                        <a class="layui-btn newBtnDefault newBtnPrimary" lay-submit lay-filter="saveForm">保存</a>
                        {/if}
                        <a class="layui-btn newBtnDefault" onclick="javascript:history.go(-1);">取消</a>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
$.ajaxSetup({ headers: { token: $.cookie('token')}});

// ********************layui相关*************************
var layer = layui.layer,
    element = layui.element,
    laydate = layui.laydate,
    laypage = layui.laypage,
    form = layui.form,
    laytpl = layui.laytpl;

form.render(); //更新全部
// 监听选择是否老客户介绍
form.on('select(selectIsOld)', function(data) {
    var value = data.value;
    checkIsOld(value)

});
// 监听选择市场推广
form.on('select(selectStatement)',function(data){
    var elem = data.elem;
    var value = data.value;
    var name = $(elem).find("option:selected").attr("data-name");
    console.log('name',name);
    $(elem).parents(".statement_box").find(".statement_name").val(name);
})

//提交表单
form.on("submit(saveForm)",function(data){
    var formData = JSON.stringify(data.field);
    $.post("{:config('api')}/Achievement/Settlement/editSettlementRelations",formData,function(res){
        console.log(res);
        if(res.code==1000){
            parent.layer.msg(res.message,{icon:1,time:2000},function(){
            window.location.href="/achievement/performance/settlementrelations"});
            
        }else{
            parent.layer.msg(res.message,{icon:2,time:2000});
        }
    })
    return false;
})

// ********************layui相关 end*************************

// ********************处理函数*************************
// 检查老客户介绍
function checkIsOld(value) {
    if (value == 1) {
        // 老系统客户 是
        $("#old_client_name").show();
        $("#old_client_tel").show();

        $(".isPushBox").hide();
        $("#push_apply_id").val('');
        $("#push_apply_name").val('');
        form.render();

    } else {
        // 老系统客户 否
        $(".isPushBox").show();

        $("#old_client_name").hide();
        $("#old_client_tel").hide();
        $("#old_client_name").val('');
        $("#old_client_tel").val('');

        form.render();
    }
}

// 获取用户列表 -- 
function getUserList(user_id = '', type, is_engineer, getType) {
    if(getType==1){
        layer.msg('正在读取数据，请稍候……', { icon: 16, shade: 0.01, shadeClose: false});
    }
    var request_data = {
        user_id: user_id,
        type: type,
        is_engineer: is_engineer
    }
    $.post("{:config('api')}/Achievement/Settlement/getAllUser",JSON.stringify(request_data), function(flag) {
        if (flag.code == 1000) {
            var data = flag.data;
            var html = '<option value="">--请选择或搜索选择--</option>';
            for (var i in data) {
                html += '<option value="' + data[i].user_id + '" data-name="' + data[i].realname + '">' + data[i].realname + '-' + data[i].telphone + '</option>'
            }
            switch (getType) {
                case 1:
                    $("#push_apply_id").html(html);
                    break;
                case 2:
                    $("#explainer_id").html(html);
                    break;
                case 3:
                    $("#manage_id").html(html);
                    $("#star_manage_id").html(html);
                    $("#two_manage_id").html(html);
                    $("#five_manage_id").html(html);
                    $("#ten_manage_id").html(html);
                    $("#twenty_manage_id").html(html);
                    break;
            }
            form.render();
            if(getType == 1){
                getUserList('', 1, 1, 2);
            }else if(getType == 2){
                getUserList('', 1, 0, 3);
            }else if( getType == 3){
                Ajaxdata();
            }
        }
    })
}


function Ajaxdata() {
    var request_data = {};
    request_data.id = $("#id").val();
    request_data = JSON.stringify(request_data);
    $.post("{:config('api')}/Achievement/Settlement/getSettlementRelations", request_data, function(flag) {
        
        console.log('flag>>>', flag);
        if (flag.code < 0) {
            layer.msg(flag.message, { icon: 2, time: 1500, shade: 0.1, });
            return false;
        }
        //基本信息 
        $("#name").val(flag.data.realname);
        $("#contract_no").val(flag.data.contract_no);
        $("#phone").val(flag.data.telphone);
        $("#device_number").val(flag.data.device_number);

        // 老客户信息
        $("#is_old_client").val(flag.data.is_old_client);
        $("#old_client_name").val(flag.data.old_client_name);
        $("#old_client_tel").val(flag.data.old_client_tel);
        checkIsOld(flag.data.is_old_client);

        // 推荐人
        $("#push_apply_id").val(flag.data.push_apply_id==0?'':flag.data.push_apply_id);
        $("#push_apply_name").val(flag.data.push_apply_name);
        // 讲解人
        var explainer_id = '';
        if(flag.data.explainer_id != 0){
            if(flag.data.explainer_type == 1){
                explainer_id = 'D-'+flag.data.explainer_id;
            }else{
                explainer_id = 'E-'+flag.data.explainer_id;
            }
        }
        $("#explainer_id").val(explainer_id);
        $("#explainer_name").val(flag.data.explainer_name);
        // 市场推广
        $("#manage_id").val(flag.data.manage_id==0?'':flag.data.manage_id);
        $("#manage_money").val(flag.data.manage_money);
        $("#manage_name").val(flag.data.manage_name);
        $("#manage_state").val(flag.data.manage_state);

        // 一星市场推广
        $("#star_manage_id").val(flag.data.star_manage_id);
        $("#star_manage_money").val(flag.data.star_manage_money);
        $("#star_manage_name").val(flag.data.star_manage_name);
        $("#star_manage_state").val(flag.data.star_manage_state);

        // 二星市场推广
        $("#two_manage_id").val(flag.data.two_manage_id);
        $("#two_manage_money").val(flag.data.two_manage_money);
        $("#two_manage_name").val(flag.data.two_manage_name);
        $("#two_manage_state").val(flag.data.two_manage_state);

        // 五星市场推广
        $("#five_manage_id").val(flag.data.five_manage_id);
        $("#five_manage_money").val(flag.data.five_manage_money);
        $("#five_manage_name").val(flag.data.five_manage_name);
        $("#five_manage_state").val(flag.data.five_manage_state);

        // 十星市场推广
        $("#ten_manage_id").val(flag.data.ten_manage_id);
        $("#ten_manage_money").val(flag.data.ten_manage_money);
        $("#ten_manage_name").val(flag.data.ten_manage_name);
        $("#ten_manage_state").val(flag.data.ten_manage_state);

        // 二十星市场推广
        $("#twenty_manage_id").val(flag.data.twenty_manage_id);
        $("#twenty_manage_money").val(flag.data.twenty_manage_money);
        $("#twenty_manage_name").val(flag.data.twenty_manage_name);
        $("#twenty_manage_state").val(flag.data.twenty_manage_state);


        if (flag.data.is_old_client == '1') {
            $("#name").val(flag.data.realname);
        }
        form.render();
        layer.closeAll();
        return false;
        $("input[name='workers_number']").val(flag.data.workers_number);
        $("input[name='workers_phone']").val(flag.data.workers_phone);

        if (flag.data.workers_birthday != 0) {
            $("input[name='birthday']").val(flag.data.workers_birthday);
        } else {
            var date = "";
            if (flag.data.workers_id_card.length == 15) {
                date = '19' + flag.data.workers_id_card.substr(6, 2) + '-' + flag.data.workers_id_card.substr(8, 2) + '-' + flag.data.workers_id_card.substr(10, 2);
            } else if (flag.data.workers_id_card.length == 18) {
                date = flag.data.workers_id_card.substr(6, 4) + '-' + flag.data.workers_id_card.substr(10, 2) + '-' + flag.data.workers_id_card.substr(12, 2);
            }
            $("input[name='birthday']").val(date);
        }
        if (flag.data.workers_entry_time != 0) {
            $("input[name='workers_entry_time']").val(flag.data.workers_entry_time);
        }
        $("input[name='inspection_standard']").val(flag.data.inspection_standard);
        $("input[name='inspection_results']").val(flag.data.inspection_results);
        $("#check_out").val(flag.data.check_out);
        $("input[name='total_money']").val(flag.data.total_money);
        $("input[name='cash_available']").val(flag.data.cash_available);
        $("input[name='already_present_money']").val(flag.data.already_present_money);
        $("input[name='commissions']").val(flag.data.commissions);
        $("input[name='invite_name']").val(flag.data.invite_name);
        $("input[name='invite_number']").val(flag.data.invite_number);
        $("input[name='invite_phone']").val(flag.data.invite_phone);

    });
}

// 判断积分输入格式
function checkMoney(str){
  var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  var value = str;
  if(str!=''){
    if (!reg.test(str)){
      value = '';
      parent.layer.msg('格式错误，请重新输入',{icon:0,time:1000});
      return value;
    }else{
      return value;
    }
  }else{
    value = '';
    return value;
  }
  
}

// ********************处理函数 end*************************

// *********************页面初始化*************************
$(function() {
    getUserList('', 2, '', 1);
})
// *********************页面初始化 end*************************
</script>